'Copyright (C) 2009 Christiaan Cable
'
'This program is free software; you can redistribute it and/or
'modify it under the terms of the GNU General Public License
'as published by the Free Software Foundation; either version 3
'of the License, or (at your option) any later version.
'
'This program is distributed in the hope that it will be useful,
'but WITHOUT ANY WARRANTY; without even the implied warranty of
'MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'GNU General Public License for more details (LICENCE.TXT).

Imports ua

Public Class frmStats
    Inherits System.Windows.Forms.Form

    Private pngUp As New Bitmap(GetType(Module1).Assembly.GetManifestResourceStream("UATrayIcon.up.png"))
    Private pngDown As New Bitmap(GetType(Module1).Assembly.GetManifestResourceStream("UATrayIcon.down.png"))
    Private CloseMnu As ContextMenu
    Private mnuItms(14) As MenuItem
    Private Xoffset As Integer
    Private Yoffset As Integer
    Private TempBitmap As Bitmap
    Private LastNicUp As Integer
    Private LastNicDown As Integer
    Public dteLastUpdated As Date
    Private NewBackColor As Color
    Public ShowError As Boolean = True
    Dim TextHeight As Integer = 15

    Private Sub frmStats_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            NewBackColor = Color.FromArgb(CInt(NewConfigurationData.BackColour))
            btnNicGraph.BackgroundImage = pngUp
            btnNicGraph.BackColor = NewBackColor
            btnNicGraph.ForeColor = NewBackColor
            pbxNicGraph.BackColor = NewBackColor
            lblError.BackColor = NewBackColor

            'initialise pictureboxes
            Dim NewBitmap As New Bitmap(pnlNICGraph.Width, pnlNICGraph.Height)
            Dim g As Graphics = Graphics.FromImage(NewBitmap)
            'draw separator
            g.DrawLine(Pens.White, 0, 0, pnlNICGraph.Width, 0)
            g.DrawLine(New Pen(Color.FromArgb(150, NewBackColor.R, NewBackColor.G, NewBackColor.B)), 0, 0, pnlNICGraph.Width, 0)
            g.Dispose()
            pnlNICGraph.BackgroundImage = NewBitmap

            pbxNicGraph.Image = New Bitmap(pbxNicGraph.Width, pbxNicGraph.Height)

            TempBitmap = New Bitmap(Me.pbxNicGraph.Width, Me.pbxNicGraph.Height)

            'set topmost
            Me.TopMost = NewConfigurationData.IconStatsTopmost

            'create a click and mousemove event for each control
            AddHandler Me.DoubleClick, AddressOf ControlMouseDoubleClick
            AddHandler Me.MouseDown, AddressOf ControlMouseDown
            AddHandler Me.MouseMove, AddressOf ControlMouseMove

            Dim NewControl1 As Control
            For Each NewControl1 In Me.Controls
                If NewControl1.GetType Is GetType(Panel) Then
                    Dim NewControl2 As Control
                    For Each NewControl2 In NewControl1.Controls
                        AddHandler NewControl2.DoubleClick, AddressOf ControlMouseDoubleClick
                        AddHandler NewControl2.MouseDown, AddressOf ControlMouseDown
                        AddHandler NewControl2.MouseMove, AddressOf ControlMouseMove
                    Next
                End If
                AddHandler NewControl1.DoubleClick, AddressOf ControlMouseDoubleClick
                AddHandler NewControl1.MouseDown, AddressOf ControlMouseDown
                AddHandler NewControl1.MouseMove, AddressOf ControlMouseMove
            Next

            'create context menus
            mnuItms(0) = New MenuItem("Close", New EventHandler(AddressOf CloseForm))
            mnuItms(1) = New MenuItem("-", New EventHandler(AddressOf Opacity100))
            mnuItms(2) = New MenuItem("Show ISP Graph", New EventHandler(AddressOf ToggleISPGraph))
            mnuItms(2).Checked = NewConfigurationData.ShowISPGraph
            mnuItms(3) = New MenuItem("Show ISP Stats", New EventHandler(AddressOf ToggleISPStats))
            mnuItms(3).Checked = NewConfigurationData.ShowISPStats
            mnuItms(4) = New MenuItem("-", New EventHandler(AddressOf Opacity100))
            mnuItms(5) = New MenuItem("Refresh ISP Usage", New EventHandler(AddressOf RefreshISPStats))
            mnuItms(6) = New MenuItem("-", New EventHandler(AddressOf Opacity100))
            mnuItms(7) = New MenuItem("Show NIC Stats", New EventHandler(AddressOf ToggleNICStats))
            mnuItms(7).Checked = NewConfigurationData.ShowNICStats
            mnuItms(8) = New MenuItem("Show NIC Graph", New EventHandler(AddressOf ToggleNICGraph))
            mnuItms(8).Checked = NewConfigurationData.IconShowNICGraph
            mnuItms(9) = New MenuItem("-", New EventHandler(AddressOf Opacity100))
            mnuItms(10) = New MenuItem("Always On Top", New EventHandler(AddressOf ToggleTopmost))
            mnuItms(10).Checked = NewConfigurationData.IconStatsTopmost
            mnuItms(11) = New MenuItem("Opacity 100%", New EventHandler(AddressOf Opacity100))
            mnuItms(12) = New MenuItem("Opacity 75%", New EventHandler(AddressOf Opacity75))
            mnuItms(13) = New MenuItem("Opacity 50%", New EventHandler(AddressOf Opacity50))
            mnuItms(14) = New MenuItem("Opacity 25%", New EventHandler(AddressOf Opacity25))

            'set menu attributes
            mnuItms(0).DefaultItem = True

            If NewConfigurationData.RetrieveNICData = False Then
                mnuItms(7).Visible = False
                mnuItms(8).Visible = False
                mnuItms(9).Visible = False
            Else
                If NewConfigurationData.LogEnabled = False Then
                    mnuItms(7).Visible = False
                End If
            End If

            CloseMnu = New ContextMenu(mnuItms)
            Me.ContextMenu = CloseMnu

            'position form
            Me.Location = New Point(NewConfigurationData.IconStartX, NewConfigurationData.IconStartY)

            'set opacity
            Me.Opacity = Val(NewConfigurationData.Opacity)

            'set opacity menu check
            mnuItms(11).Checked = False
            mnuItms(12).Checked = False
            mnuItms(13).Checked = False
            mnuItms(14).Checked = False
            If NewConfigurationData.Opacity = "1" Then
                mnuItms(11).Checked = True
            ElseIf NewConfigurationData.Opacity = "0.75" Then
                mnuItms(12).Checked = True
            ElseIf NewConfigurationData.Opacity = "0.5" Then
                mnuItms(13).Checked = True
            ElseIf NewConfigurationData.Opacity = "0.25" Then
                mnuItms(14).Checked = True
            End If

            'set panel text
            lblError.Visible = True
            lblError.Text = "Fetching ISP Data"

            'set visibility for Usage bitmap
            If NewConfigurationData.ShowISPGraph = True Or NewConfigurationData.ShowISPStats = True Or NewConfigurationData.ShowNICStats = True Then
                pbxUsage.Visible = True
                lblError.Visible = True
            Else
                pbxUsage.Visible = False
                lblError.Visible = False
            End If

            'set visibility for NIC panel
            If NewConfigurationData.RetrieveNICData = False Then
                pnlNICGraph.Visible = False
            Else
                pnlNICGraph.Visible = NewConfigurationData.IconShowNICGraph
            End If

            'set nic values font
            lblCurrentDownRate.Font = New Font(NewConfigurationData.Font, CSng(NewConfigurationData.FontSize * (96 / GetDPI())), FontStyle.Regular)
            lblCurrentUpRate.Font = New Font(NewConfigurationData.Font, CSng(NewConfigurationData.FontSize * (96 / GetDPI())), FontStyle.Regular)

            UpdateForm()

            Timer1.Enabled = True

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
            Me.Close()
        End Try
    End Sub

    Private Sub frmStats_FormClosed(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        'clean up
        TempBitmap.Dispose()
    End Sub

    Private Sub frmStats_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Timer1.Enabled = False
        Timer1.Dispose()
    End Sub

    Private Sub frmStats_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
        NewConfigurationData.IconStartX = Me.Left
        NewConfigurationData.IconStartY = Me.Top
    End Sub

    Private Sub CloseForm(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.Close()
    End Sub

    Private Sub Opacity100(ByVal sender As Object, ByVal e As System.EventArgs)
        NewConfigurationData.Opacity = "1"
        Me.Opacity = Val(NewConfigurationData.Opacity)
        mnuItms(11).Checked = True
        mnuItms(12).Checked = False
        mnuItms(13).Checked = False
        mnuItms(14).Checked = False
    End Sub

    Private Sub Opacity75(ByVal sender As Object, ByVal e As System.EventArgs)
        NewConfigurationData.Opacity = "0.75"
        Me.Opacity = Val(NewConfigurationData.Opacity)
        'set opacity menu check
        mnuItms(11).Checked = False
        mnuItms(12).Checked = True
        mnuItms(13).Checked = False
        mnuItms(14).Checked = False
    End Sub

    Private Sub Opacity50(ByVal sender As Object, ByVal e As System.EventArgs)
        NewConfigurationData.Opacity = "0.5"
        Me.Opacity = Val(NewConfigurationData.Opacity)
        mnuItms(11).Checked = False
        mnuItms(12).Checked = False
        mnuItms(13).Checked = True
        mnuItms(14).Checked = False
    End Sub

    Private Sub Opacity25(ByVal sender As Object, ByVal e As System.EventArgs)
        NewConfigurationData.Opacity = "0.25"
        Me.Opacity = Val(NewConfigurationData.Opacity)
        mnuItms(11).Checked = False
        mnuItms(12).Checked = False
        mnuItms(13).Checked = False
        mnuItms(14).Checked = True
    End Sub

    Private Sub ToggleISPGraph(ByVal sender As Object, ByVal e As System.EventArgs)
        If NewConfigurationData.ShowISPGraph = True Then
            'ignore if last left
            If NewConfigurationData.ShowISPStats = True Or NewConfigurationData.ShowNICStats = True Or NewConfigurationData.IconShowNICGraph = True Then
                'hide
                mnuItms(2).Checked = False
                NewConfigurationData.ShowISPGraph = False
            End If
        Else
            'show
            mnuItms(2).Checked = True
            NewConfigurationData.ShowISPGraph = True
        End If
        'force refresh
        dteLastUpdated = Now
    End Sub

    Private Sub ToggleISPStats(ByVal sender As Object, ByVal e As System.EventArgs)
        If NewConfigurationData.ShowISPStats = True Then
            'ignore if last left
            If NewConfigurationData.ShowISPGraph = True Or NewConfigurationData.ShowNICStats = True Or NewConfigurationData.IconShowNICGraph = True Then
                'hide
                mnuItms(3).Checked = False
                NewConfigurationData.ShowISPStats = False
            End If
        Else
            'show
            mnuItms(3).Checked = True
            NewConfigurationData.ShowISPStats = True
        End If
        'force refresh
        dteLastUpdated = Now
    End Sub

    Private Sub ToggleNICStats(ByVal sender As Object, ByVal e As System.EventArgs)
        If NewConfigurationData.ShowNICStats = True Then
            'ignore if last left
            If NewConfigurationData.ShowISPGraph = True Or NewConfigurationData.ShowISPStats = True Or NewConfigurationData.IconShowNICGraph = True Then
                'hide
                mnuItms(7).Checked = False
                NewConfigurationData.ShowNICStats = False
            End If
        Else
            'show
            mnuItms(7).Checked = True
            NewConfigurationData.ShowNICStats = True
        End If
        'force refresh
        dteLastUpdated = Now
    End Sub

    Private Sub ToggleNICGraph(ByVal sender As Object, ByVal e As System.EventArgs)
        If NewConfigurationData.IconShowNICGraph = True Then
            'ignore if last left
            If NewConfigurationData.ShowISPGraph = True Or NewConfigurationData.ShowISPStats = True Or NewConfigurationData.ShowNICStats = True Then
                'hide
                mnuItms(8).Checked = False
                NewConfigurationData.IconShowNICGraph = False
                UpdateBitmap()
            End If
        Else
            'show
            mnuItms(8).Checked = True
            NewConfigurationData.IconShowNICGraph = True
            UpdateBitmap()
        End If
    End Sub

    Sub ToggleTopmost(ByVal sender As Object, ByVal e As System.EventArgs)
        If NewConfigurationData.IconStatsTopmost = True Then
            NewConfigurationData.IconStatsTopmost = False
        Else
            NewConfigurationData.IconStatsTopmost = True
        End If

        mnuItms(10).Checked = NewConfigurationData.IconStatsTopmost
        Me.TopMost = NewConfigurationData.IconStatsTopmost
    End Sub

    Private Sub RefreshISPStats(ByVal sender As Object, ByVal e As System.EventArgs)
        RetrieveISPStats()
    End Sub

    Private Sub ControlMouseDoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.DoubleClick
        Me.Close()
    End Sub

    Private Sub ControlMouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        Xoffset = e.X
        Yoffset = e.Y
    End Sub

    Private Sub ControlMouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)
        If e.Button = Windows.Forms.MouseButtons.Left Then MoveEvent(e.X, e.Y)
    End Sub

    Private Sub MoveEvent(ByVal x As Integer, ByVal y As Integer)
        If (Me.Left + x - Xoffset < 10) And (Me.Left + x - Xoffset > -10) Then
            Me.Left = 0
        ElseIf (Me.Left + x - Xoffset > SystemInformation.WorkingArea.Width - Me.Width - 10) And (Me.Left + x - Xoffset < SystemInformation.WorkingArea.Width - Me.Width + 10) Then
            Me.Left = SystemInformation.WorkingArea.Width - Me.Width
        Else
            Me.Left += x - Xoffset
        End If

        If Me.Top + y - Yoffset < 10 Then
            Me.Top = 0
        ElseIf Me.Top + y - Yoffset > SystemInformation.WorkingArea.Height - Me.Height - 10 Then
            Me.Top = SystemInformation.WorkingArea.Height - Me.Height
        Else
            Me.Top += y - Yoffset
        End If

        'save
        NewConfigurationData.IconStartX = Me.Left
        NewConfigurationData.IconStartY = Me.Top
    End Sub

    Private Sub btnNicGraph_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNicGraph.Click
        If btnNicGraph.BackgroundImage Is pngUp Then
            btnNicGraph.BackgroundImage = pngDown
        Else
            btnNicGraph.BackgroundImage = pngUp
        End If
    End Sub

    Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        Try
            UpdateForm()
        Catch ex As Exception
            Timer1.Stop()
            MessageBox.Show(ex.Message, "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Private Sub UpdateForm()
        Try
            If ISPReturnData IsNot Nothing Then
                'fetching
                If ISPReturnData.Status = 1 Then
                    'only show error if visible
                    If NewConfigurationData.ShowISPGraph = True Or NewConfigurationData.ShowISPStats = True Or NewConfigurationData.ShowNICStats = True Then
                        If Val(NewConfigurationData.IconUpdateDelay) > 0 And Val(NewConfigurationData.IconUpdateDelay) < 61 Then
                            'waiting
                            lblError.Visible = True
                            lblError.Text = "Delaying for " & NewConfigurationData.IconUpdateDelay & " Seconds"
                         End If
                    End If

                ElseIf ISPReturnData.Status = 3 And ShowError = True Then
                    ShowError = False

                    'hide error label
                    lblError.Visible = False

                    'Draw error
                    UpdateBitmap()
                Else
                    'hide error label
                    lblError.Visible = False

                    'check if ISP data has changed
                    If Date.Compare(dteLastUpdated, ISPReturnData.LastUpdated) <> 0 Then
                        dteLastUpdated = ISPReturnData.LastUpdated

                        'DrawUsage
                        UpdateBitmap()
                    End If
                End If
            End If

            If NewConfigurationData.RetrieveNICData = True Then

                'NIC Graph
                If NewConfigurationData.IconShowNICGraph = True Then
                    'if pnlnicgraph is not visible, then clear before showing
                    If pnlNICGraph.Visible = False Then
                        Dim newg2 As Graphics = Graphics.FromImage(pbxNicGraph.Image)
                        newg2.Clear(NewBackColor)
                        newg2.Dispose()
                    End If

                    'adjust size
                    If pbxNicGraph.Image.Width <> pbxNicGraph.Width Then
                        pnlNICGraph.BackgroundImage.Dispose()
                        pnlNICGraph.BackgroundImage = Nothing
                        SetNICGraphBackground()

                        'resize tempbitmap
                        TempBitmap.Dispose()
                        TempBitmap = New Bitmap(pbxNicGraph.Width, pbxNicGraph.Height)
                        'resize pbxNicGraph image
                        pbxNicGraph.Image.Dispose()
                        pbxNicGraph.Image = New Bitmap(pbxNicGraph.Width, pbxNicGraph.Height)
                    End If

                    If NewConfigurationData.ShowISPGraph = True Or NewConfigurationData.ShowISPStats = True Or NewConfigurationData.ShowNICStats = True Then
                        If btnNicGraph.BackgroundImage Is pngDown Then
                            If pbxNicGraph.Visible = True Then SetNICGraphBackground()
                            pbxNicGraph.Visible = False
                            pnlNICGraph.Visible = False
                            Me.Height = pbxUsage.Height + TextHeight + 2
                            If pnlNICGraph.Top <> pbxUsage.Height Then
                                pnlNICGraph.BackgroundImage.Dispose()
                                pnlNICGraph.BackgroundImage = Nothing
                                SetNICGraphBackground()
                                pnlNICGraph.Top = pbxUsage.Height
                            End If
                            pnlNICGraph.Visible = True
                        Else
                            If pbxNicGraph.Visible = False Then SetNICGraphBackground()
                            Me.Height = pbxUsage.Height + pnlNICGraph.Height
                            pbxNicGraph.Visible = True
                            If pnlNICGraph.Top <> pbxUsage.Height Then
                                pnlNICGraph.BackgroundImage.Dispose()
                                pnlNICGraph.BackgroundImage = Nothing
                                SetNICGraphBackground()
                                pnlNICGraph.Top = pbxUsage.Height
                            End If
                        End If
                    Else
                        If btnNicGraph.BackgroundImage Is pngDown Then
                            Me.Height = 14
                            If pbxNicGraph.Visible = True Then SetNICGraphBackground()
                            pbxNicGraph.Visible = False
                        Else
                            Me.Height = pnlNICGraph.Height
                            If pbxNicGraph.Visible = False Then SetNICGraphBackground()
                            pbxNicGraph.Visible = True
                        End If
                        pnlNICGraph.Top = -1
                    End If
                    'refresh background
                    SetNICGraphBackground()

                    pnlNICGraph.Visible = True

                    Dim NicUp As Integer = CInt(((NicUsageUp - LastNicUsageUp) / (NewConfigurationData.IconNICCapacityUp * 1024)) * (Me.pbxNicGraph.Height - 2))
                    Dim NicDown As Integer = CInt(((NicUsageDown - LastNicUsageDown) / (NewConfigurationData.IconNICCapacityDown * 1024)) * (Me.pbxNicGraph.Height - 2))

                    Me.lblCurrentUpRate.Text = "U/L: " & Microsoft.VisualBasic.Left(Format((NicUsageUp - LastNicUsageUp) / 1024, "0.00"), 4) & " kB/s"
                    Me.lblCurrentDownRate.Text = "D/L: " & Microsoft.VisualBasic.Left(Format((NicUsageDown - LastNicUsageDown) / 1024, "0.00"), 4) & " kB/s"

                    Dim mybrush As Brush
                    Dim g As Graphics = Graphics.FromImage(pbxNicGraph.Image)
                    Dim TempBitmapGraphics As Graphics = Graphics.FromImage(TempBitmap)

                    'Create rectangle for source image.
                    Dim srcRect As New Rectangle(1, 0, TempBitmap.Width, TempBitmap.Height)

                    'Create rectangle for target image.
                    Dim destRect As New Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height)

                    'Draw image to temp bitmap.
                    TempBitmapGraphics.DrawImage(pbxNicGraph.Image, destRect, srcRect, GraphicsUnit.Pixel)

                    'Create rectangle for source image.
                    srcRect = New Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height)

                    'Create rectangle for target image.
                    destRect = New Rectangle(0, 0, TempBitmap.Width, TempBitmap.Height)

                    'Draw image to screen.
                    g.DrawImage(TempBitmap, destRect, srcRect, GraphicsUnit.Pixel)

                    'clear background
                    mybrush = New SolidBrush(NewBackColor)
                    g.FillRectangle(mybrush, pbxNicGraph.Width - 1, 0, 2, pbxNicGraph.Height)

                    g.SmoothingMode = Drawing2D.SmoothingMode.HighQuality
                    g.InterpolationMode = Drawing2D.InterpolationMode.HighQualityBicubic

                    'draw nic value down
                    If NicDown > 0 Or LastNicDown > 0 Then
                        If NicDown > Me.pbxNicGraph.Height Then NicDown = Me.pbxNicGraph.Height
                        If NicDown = 0 Then
                            g.DrawLine(New Pen(Color.LightGreen), Me.pbxNicGraph.Width - 3, Me.pbxNicGraph.Height - LastNicDown, Me.pbxNicGraph.Width - 2, Me.pbxNicGraph.Height)
                        ElseIf LastNicDown = 0 Then
                            g.DrawLine(New Pen(Color.LightGreen), Me.pbxNicGraph.Width - 3, Me.pbxNicGraph.Height, Me.pbxNicGraph.Width - 2, Me.pbxNicGraph.Height - NicDown)
                        Else
                            g.DrawLine(New Pen(Color.LightGreen), Me.pbxNicGraph.Width - 3, Me.pbxNicGraph.Height - LastNicDown, Me.pbxNicGraph.Width - 2, Me.pbxNicGraph.Height - NicDown)
                        End If
                    End If

                    If NicUp > 0 Or LastNicUp > 0 Then
                        If NicUp > Me.pbxNicGraph.Height Then NicUp = Me.pbxNicGraph.Height
                        If NicUp = 0 Then
                            g.DrawLine(New Pen(Color.LightCoral), Me.pbxNicGraph.Width - 3, Me.pbxNicGraph.Height - LastNicUp, Me.pbxNicGraph.Width - 2, Me.pbxNicGraph.Height)
                        ElseIf LastNicUp = 0 Then
                            g.DrawLine(New Pen(Color.LightCoral), Me.pbxNicGraph.Width - 3, Me.pbxNicGraph.Height, Me.pbxNicGraph.Width - 2, Me.pbxNicGraph.Height - NicUp)
                        Else
                            g.DrawLine(New Pen(Color.LightCoral), Me.pbxNicGraph.Width - 3, Me.pbxNicGraph.Height - LastNicUp, Me.pbxNicGraph.Width - 2, Me.pbxNicGraph.Height - NicUp)
                        End If
                    End If

                    'refresh screen
                    If pbxNicGraph.Visible = True Then pbxNicGraph.Refresh()

                    'clean up
                    g.Dispose()
                    TempBitmapGraphics.Dispose()

                    'save data
                    LastNicDown = NicDown
                    LastNicUp = NicUp
                Else
                    'not visible so resize
                    pnlNICGraph.Visible = NewConfigurationData.IconShowNICGraph
                    Me.Height = pbxUsage.Height
                End If
            End If

        Catch ex As Exception
            MessageBox.Show(ex.Message, "Usage Agent", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
        End Try
    End Sub

    Sub UpdateBitmap()
        'DrawUsage

        'get form size
        Dim FormSize As Point
        If NewConfigurationData.ShowISPGraph = True Or NewConfigurationData.ShowISPStats = True Or NewConfigurationData.ShowNICStats = True Then
            FormSize = CalculateFormSize(NewConfigurationData, ISPReturnData)
            pbxUsage.Width = FormSize.X
            pbxUsage.Height = FormSize.Y

            'resize error
            lblError.Size = CType(New Point(FormSize.X, FormSize.Y), Size)

            'dispose previous
            If pbxUsage.Image IsNot Nothing Then pbxUsage.Image.Dispose()

            'create bitmap
            Dim UABitmap As New Bitmap(FormSize.X, FormSize.Y)

            'draw ISP form
            If NewConfigurationData.RetrieveNICData = True And NewConfigurationData.IconShowNICGraph = True Then
                DrawISPForm(UABitmap, ISPReturnData, NewConfigurationData, True)
            Else
                DrawISPForm(UABitmap, ISPReturnData, NewConfigurationData)
            End If

            'resize
            Me.Width = FormSize.X
            If NewConfigurationData.RetrieveNICData = True And NewConfigurationData.IconShowNICGraph = False Then
                Me.Height = FormSize.Y + pnlNICGraph.Height
                pnlNICGraph.Top = FormSize.Y
                pnlNICGraph.BackgroundImage.Dispose()
                pnlNICGraph.BackgroundImage = Nothing
                SetNICGraphBackground()
            Else
                Me.Height = FormSize.Y
            End If

            'calculate size of nic text
            Dim TempBitmap As New Bitmap(1, 1)
            Dim g As Graphics = Graphics.FromImage(TempBitmap)
            Dim RegularFont As New Font(NewConfigurationData.Font, CSng(NewConfigurationData.FontSize * (96 / GetDPI())), FontStyle.Regular)
            Dim TextWidth As Integer = CInt(g.MeasureString("D/L: 0.00 kB/s", RegularFont, 1000, StringFormat.GenericTypographic).Width)
            TextHeight = CInt(g.MeasureString("D/L: 0.00 kB/s", RegularFont, 1000, StringFormat.GenericTypographic).Height)
            TempBitmap.Dispose()
            RegularFont.Dispose()

            'resize nic panel
            pnlNICGraph.Height = TextHeight + 6 + 52

            'reposition nic values
            lblCurrentDownRate.Left = CInt((pbxUsage.Width - btnNicGraph.Width - (TextWidth * 2)) / 4)
            lblCurrentUpRate.Left = CInt((((pbxUsage.Width - (TextWidth * 2)) / 4) * 3) + TextWidth)

            'assign new bitmap
            pbxUsage.Image = UABitmap
            pbxUsage.Visible = True
        Else
            Me.Height = pnlNICGraph.Height - 1
            pbxUsage.Visible = False
            pnlNICGraph.Top = -1
            pnlNICGraph.BackgroundImage.Dispose()
            pnlNICGraph.BackgroundImage = Nothing
            SetNICGraphBackground()
        End If
    End Sub

    Private Sub SetNICGraphBackground()
        'if current size = new size then exit
        If pnlNICGraph.BackgroundImage IsNot Nothing Then
            If btnNicGraph.BackgroundImage Is pngDown Then
                If pnlNICGraph.BackgroundImage.Height = TextHeight + 2 Then Exit Sub
            Else
                If pnlNICGraph.BackgroundImage.Height > TextHeight + 2 Then Exit Sub
            End If
            pnlNICGraph.BackgroundImage.Dispose()
        End If

        'set backgound image
        Dim NewBitmap As Bitmap

        If btnNicGraph.BackgroundImage Is pngDown Then
            NewBitmap = New Bitmap(pnlNICGraph.Width, TextHeight + 2)
            Dim newg As Graphics = Graphics.FromImage(NewBitmap)
            'draw separator
            newg.DrawLine(Pens.White, 0, 0, pnlNICGraph.Width, 0)
            newg.DrawLine(New Pen(Color.FromArgb(150, NewBackColor.R, NewBackColor.G, NewBackColor.B)), 0, 0, pnlNICGraph.Width, 0)

            If NewBitmap.Height >= Me.Height Then
                DrawRoundedRectangle(newg, 0, 1, NewBitmap.Width - 1, NewBitmap.Height - 2, NewBackColor, True, True)
            Else
                DrawRoundedRectangle(newg, 0, 1, NewBitmap.Width - 1, NewBitmap.Height - 2, NewBackColor, False, True)
            End If

            newg.Dispose()
        Else
            NewBitmap = New Bitmap(pnlNICGraph.Width, pnlNICGraph.Height)
            Dim newg As Graphics = Graphics.FromImage(NewBitmap)
            'draw separator
            newg.DrawLine(Pens.White, 0, 0, pnlNICGraph.Width, 0)
            newg.DrawLine(New Pen(Color.FromArgb(150, NewBackColor.R, NewBackColor.G, NewBackColor.B)), 0, 0, pnlNICGraph.Width, 0)

            If NewBitmap.Height >= Me.Height Then
                DrawRoundedRectangle(newg, 0, 1, NewBitmap.Width - 1, NewBitmap.Height - 2, NewBackColor, True, True)
            Else
                DrawRoundedRectangle(newg, 0, 1, NewBitmap.Width - 1, NewBitmap.Height - 2, NewBackColor, False, True)
            End If

            newg.Dispose()
        End If

        pnlNICGraph.BackgroundImage = NewBitmap
    End Sub
End Class
